// src/scenarios/supply_chain.rs

//! 供应链管理场景
//! 支持供应链管理的语义转换和OPC UA模型构建

use crate::scenarios::*;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// 供应链管理场景
pub struct SupplyChainScenario {
    config: ScenarioConfig,
}

/// 物流状态
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum LogisticsStatus {
    /// 待发货
    Pending,
    /// 运输中
    InTransit,
    /// 已送达
    Delivered,
    /// 延迟
    Delayed,
    /// 异常
    Exception,
}

/// 库存状态
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum InventoryStatus {
    /// 正常
    Normal,
    /// 库存不足
    LowStock,
    /// 缺货
    OutOfStock,
    /// 积压
    Overstock,
}

impl SupplyChainScenario {
    pub fn new() -> Self {
        Self {
            config: ScenarioConfig {
                name: Some("供应链管理语义转换场景".to_string()),
                description: Some("支持供应链管理数据的语义转换和OPC UA模型构建，包括物流跟踪、库存管理、供应商管理等".to_string()),
                custom_rules: None,
                extensions: HashMap::new(),
            },
        }
    }

    pub fn new_with_config(config: ScenarioConfig) -> Self {
        Self { config }
    }
}

impl Scenario for SupplyChainScenario {
    fn name(&self) -> &'static str {
        "Supply Chain Management Semantic Conversion"
    }

    fn description(&self) -> &'static str {
        "供应链管理语义转换场景，支持物流跟踪、库存管理等功能的语义转换"
    }

    fn industry(&self) -> &'static str {
        "SupplyChain"
    }

    fn get_transformation_rules(&self) -> ScenarioTransformationRules {
        ScenarioTransformationRules {
            rule_name: "SupplyChain_General".to_string(),
            data_source_mappings: HashMap::new(),
            unit_conversions: HashMap::new(),
            data_type_mappings: HashMap::new(),
            semantic_mappings: HashMap::new(),
            constraints: vec![],
        }
    }

    fn get_opcua_model_template(&self) -> ScenarioModelTemplate {
        ScenarioModelTemplate {
            template_name: "SupplyChainModel".to_string(),
            namespaces: vec![
                "http://opcfoundation.org/UA/".to_string(),
                "http://industryagent.com/supplychain/".to_string(),
            ],
            object_types: vec![
                ObjectTypeTemplate {
                    type_name: "SupplyChainNodeType".to_string(),
                    description: Some("供应链节点类型".to_string()),
                    parent_type: Some("BaseObjectType".to_string()),
                    properties: vec![
                        PropertyTemplate {
                            name: "NodeId".to_string(),
                            data_type: "String".to_string(),
                            description: Some("节点ID".to_string()),
                            unit: None,
                            required: true,
                            default_value: None,
                        },
                        PropertyTemplate {
                            name: "NodeType".to_string(),
                            data_type: "String".to_string(),
                            description: Some("节点类型".to_string()),
                            unit: None,
                            required: true,
                            default_value: None,
                        },
                    ],
                    methods: vec![],
                },
            ],
            variable_types: vec![],
            reference_types: vec![],
        }
    }

    fn get_sample_data(&self) -> Vec<ScenarioSampleData> {
        vec![
            ScenarioSampleData {
                data_id: "shipment_001".to_string(),
                data_source_type: "HuaweiCloudIoT".to_string(),
                raw_data: serde_json::json!({
                    "shipment_id": "SHP-001",
                    "origin": "Shanghai",
                    "destination": "Beijing",
                    "status": "InTransit",
                    "estimated_arrival": "2024-01-16T10:00:00Z",
                    "current_location": "Nanjing",
                    "items_count": 150,
                    "weight": 850.5,
                    "timestamp": "2024-01-15T14:30:00Z"
                }),
                expected_opcua: serde_json::json!({
                    "nodeId": "ns=4;i=1001",
                    "nodeClass": "Object",
                    "browseName": "SHP-001"
                }),
                description: Some("物流运输数据示例".to_string()),
                category: Some("Logistics".to_string()),
            },
        ]
    }

    fn get_semantic_mappings(&self) -> HashMap<String, String> {
        let mut mappings = HashMap::new();
        mappings.insert("warehouse".to_string(), "StorageFacility".to_string());
        mappings.insert("distribution_center".to_string(), "DistributionCenter".to_string());
        mappings
    }
}